package com.wlc.controller;

import com.wlc.pojo.LeaveApplication;
import com.wlc.pojo.PageResult;
import com.wlc.pojo.Result;
import com.wlc.service.LeaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/leave")
@Slf4j
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    /**
     * 分页查询请假列表
     */
    @GetMapping("/list")
    public Result<PageResult<LeaveApplication>> list(
            @RequestParam(required = false) Long applicantId,
            @RequestParam(required = false) Integer leaveType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("分页查询请假列表: applicantId={}, leaveType={}, status={}, startDate={}, endDate={}, page={}, size={}",
                applicantId, leaveType, status, startDate, endDate, page, size);
        try {
            PageResult<LeaveApplication> pageResult = leaveService.findLeaveList(
                    applicantId, leaveType, status, startDate, endDate, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("查询请假列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据ID查询请假
     */
    @GetMapping("/{id}")
    public Result<LeaveApplication> getById(@PathVariable Long id) {
        log.info("根据ID查询请假: {}", id);
        try {
            LeaveApplication leave = leaveService.findById(id);
            return Result.success(leave);
        } catch (Exception e) {
            log.error("查询请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 新增请假
     */
    @PostMapping
    public Result<String> save(@RequestBody LeaveApplication leaveApplication) {
        log.info("新增请假: 类型={}", leaveApplication.getLeaveType());
        try {
            // TODO: 从登录信息中获取当前用户信息
            leaveApplication.setApplicantId(1L);
            leaveApplication.setApplicantName("当前用户");

            leaveService.save(leaveApplication);
            return Result.success("申请请假成功");
        } catch (Exception e) {
            log.error("新增请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 更新请假
     */
    @PutMapping
    public Result<String> update(@RequestBody LeaveApplication leaveApplication) {
        log.info("更新请假: {}", leaveApplication.getId());
        try {
            leaveService.update(leaveApplication);
            return Result.success("更新请假成功");
        } catch (Exception e) {
            log.error("更新请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除请假
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除请假: {}", id);
        try {
            leaveService.deleteById(id);
            return Result.success("删除请假成功");
        } catch (Exception e) {
            log.error("删除请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 撤销请假
     */
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id) {
        log.info("撤销请假: {}", id);
        try {
            // TODO: 从登录信息中获取当前用户ID
            Long currentUserId = 1L;

            leaveService.cancelLeave(id, currentUserId);
            return Result.success("撤销请假成功");
        } catch (Exception e) {
            log.error("撤销请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取请假类型列表
     */
    @GetMapping("/types")
    public Result<List<Map<String, Object>>> getLeaveTypes() {
        log.info("获取请假类型列表");
        try {
            List<Map<String, Object>> types = leaveService.getLeaveTypes();
            return Result.success(types);
        } catch (Exception e) {
            log.error("获取请假类型列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 审批请假
     */
    @PutMapping("/approve")
    public Result<String> approve(@RequestBody Map<String, Object> approveData) {
        log.info("审批请假: {}", approveData);
        try {
            Long id = Long.valueOf(approveData.get("id").toString());
            Integer status = Integer.valueOf(approveData.get("status").toString());
            String approveRemark = (String) approveData.get("approveRemark");

            // TODO: 从登录信息中获取当前审批人信息
            Long approverId = 1L;
            String approverName = "当前审批人";

            leaveService.approveLeave(id, status, approveRemark, approverId, approverName);

            String message = status == 1 ? "审批通过成功" : "审批拒绝成功";
            return Result.success(message);
        } catch (Exception e) {
            log.error("审批请假失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取我的请假列表
     */
    @GetMapping("/my")
    public Result<PageResult<LeaveApplication>> myLeaveList(
            @RequestParam(required = false) Integer leaveType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("查询我的请假列表: leaveType={}, status={}, startDate={}, endDate={}, page={}, size={}",
                leaveType, status, startDate, endDate, page, size);
        try {
            // TODO: 从登录信息中获取当前用户ID
            Long currentUserId = 1L;

            PageResult<LeaveApplication> pageResult = leaveService.findLeaveList(
                    currentUserId, leaveType, status, startDate, endDate, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("查询我的请假列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取待我审批的请假列表
     */
    @GetMapping("/approval/pending")
    public Result<PageResult<LeaveApplication>> pendingApprovalList(
            @RequestParam(required = false) Integer leaveType,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        log.info("查询待审批请假列表: leaveType={}, startDate={}, endDate={}, page={}, size={}",
                leaveType, startDate, endDate, page, size);
        try {
            // 查询待审批状态的请假
            PageResult<LeaveApplication> pageResult = leaveService.findLeaveList(
                    null, leaveType, 0, startDate, endDate, page, size);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("查询待审批请假列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}